package com.jxt.admin.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jxt.app.entity.base.Apmessages;
import com.jxt.app.entity.base.ApmessagesExample;
import com.jxt.app.service.ApDialogService;
import com.jxt.app.service.ApMessageService;
import com.jxt.app.util.MainApnsSend;
import com.jxt.app.util.Page;

@Controller
public class MessageController {

	// 日志记录器
	private static final Logger logger = Logger.getLogger(MessageController.class);


	
	@Resource
	private ApMessageService apMessageService;
	
	@RequestMapping(value="/app/msg/html/list",method=RequestMethod.GET) 
	public String listpage(HttpServletRequest requset,@RequestParam(required=false, defaultValue="1") int page,@RequestParam(required=false, defaultValue="10") int pageSize,HttpServletResponse response, Model model) throws UnsupportedEncodingException{
	
		
		
		return "msg";
	}
	
	
	/**
	 * 关键字查询
	 * @param requset
	 * @param model
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/Apmessages/extfield.do",method=RequestMethod.POST) 
	public void extfield(ApmessagesExample apmessage,String msg,String field_type,HttpServletRequest request,@RequestParam(required=false, defaultValue="1") int start,@RequestParam(required=false, defaultValue="20") int limit,@RequestParam(required=false, defaultValue="1") int page,@RequestParam(required=false, defaultValue="10") int pageSize,HttpServletResponse response, Model model) throws UnsupportedEncodingException{
		
		
		page=start/limit+1;
		pageSize=limit;
		response.setContentType("text/html; charset=UTF-8");
		JSONObject obj = new JSONObject();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			ApmessagesExample exmaple=new ApmessagesExample();
			exmaple.setId((long)0);
			exmaple.setId_Indicator(ApmessagesExample.EXAMPLE_GREATER_THAN);
			
			
			if(apmessage!=null){
				exmaple.setMsg(apmessage.getMsg());
				exmaple.setMsg_Indicator(ApmessagesExample.EXAMPLE_LIKE);
			}
			
			
			Page pageList=apMessageService.findListByExample(exmaple, page, pageSize);
			

			
			
			
			
			obj.put("code", 1001);
			obj.put("result", obj);
			obj.put("list", pageList.getResult());
			obj.put("totalSize", pageList.getRowCount());
			obj.put("success", true);
			obj.put("msg", "成 功!");
			out.print(obj);
			
	
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			obj.put("failure", true);
			obj.put("msg", "异常!");
			e1.printStackTrace();
			out.print(obj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			obj.put("failure", true);
			obj.put("msg", "异常!");
			e.printStackTrace();
			out.print(obj);
		} 

		
		
	
	}
	
	
	/**
	 * 查询
	 * @param requset
	 * @param model
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/Apmessages/extlist.do",method=RequestMethod.POST) 
	public void list(HttpServletRequest request,String msg,String field_type,int start,@RequestParam(required=false, defaultValue="20") int limit,@RequestParam(required=false, defaultValue="1") int page,@RequestParam(required=false, defaultValue="10") int pageSize,HttpServletResponse response, Model model) throws UnsupportedEncodingException{
		
		
		page=start/limit+1;
		pageSize=limit;
		response.setContentType("text/html; charset=UTF-8");
		JSONObject obj = new JSONObject();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			ApmessagesExample exmaple=new ApmessagesExample();
			exmaple.setId((long)0);
			exmaple.setId_Indicator(ApmessagesExample.EXAMPLE_GREATER_THAN);
			

			
			Page pageList=apMessageService.findListByExample(exmaple, page, pageSize);
			

			
			
			
			
			obj.put("code", 1001);
			obj.put("result", obj);
			obj.put("list", pageList.getResult());
			obj.put("totalSize", pageList.getRowCount());
			obj.put("success", true);
			obj.put("msg", "成 功!");
			out.print(obj);
			
	
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			obj.put("failure", true);
			obj.put("msg", "异常!");
			e1.printStackTrace();
			out.print(obj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			obj.put("failure", true);
			obj.put("msg", "异常!");
			e.printStackTrace();
			out.print(obj);
		} 

		
		
	
	}
	/**
	 * 更新
	 * @param requset
	 * @param apmessage
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value="/Apmessages/extupdate.do",method=RequestMethod.POST) 
	public void extupdate(HttpServletRequest requset,ApmessagesExample apmessage,HttpServletResponse response, Model model) throws IOException{
	
		response.setContentType("text/html; charset=UTF-8"); 
		JSONObject obj = new JSONObject();
		PrintWriter out = null;
		out = response.getWriter();
		try
		{
			apMessageService.updateModel(apmessage);
			obj.put("success", true);
			obj.put("msg", "修改成功!");
			out.print(obj);
		}
		catch (Exception e)
		{
			obj.put("failure", true);
			obj.put("msg", e.getMessage());
			e.printStackTrace();
			out.print(obj);
		}
		
	
	}
	
	
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value="/Apmessages/extdelete.do",method=RequestMethod.POST) 
	public void extdelete(HttpServletRequest request,HttpServletResponse response, Model model) throws IOException{
		
		response.setContentType("text/html; charset=UTF-8"); 
		JSONObject obj = new JSONObject();
		PrintWriter out = null;
		out = response.getWriter();
		

		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Long id = new java.lang.Long((String)idarray[i]);
				ApmessagesExample apmessage=new ApmessagesExample();
				apmessage.setId(id);
				apmessage.setId_Indicator(ApmessagesExample.EXAMPLE_EQUALS);
				apMessageService.deleteModel(apmessage);
			}
			obj.put("success", true);
			obj.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			obj.put("failure", true);
			obj.put("msg", e.getMessage());
			e.printStackTrace();
		}
		out.print(obj);
		

	}
	
	
	/**
	 * 保存
	 * @param apmessage
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	@RequestMapping(value="/Apmessages/extsave.do",method=RequestMethod.POST) 
	public void extsave(ApmessagesExample apmessage,HttpServletRequest request,HttpServletResponse response, Model model) throws IOException{
		
		response.setContentType("text/html; charset=UTF-8"); 
		JSONObject obj = new JSONObject();
		PrintWriter out = null;
		out = response.getWriter();
		try
		{
			int ret=apMessageService.insertModel(apmessage);
			if(ret==1){
				obj.put("success", true);
				obj.put("msg", "添 加 成 功!");
			}else{
				obj.put("failure", true);
				obj.put("msg","添 加 失 败!");
				
			}
		}
		catch (Exception e)
		{
			obj.put("failure", true);
			obj.put("msg", e.getMessage());
			e.printStackTrace();
		}
		out.print(obj);

	}
	
	
	///Apmessages/extupdate.do /Apmessages/extdelete.do /Apmessages/extsave.do
		
	
}
